Swift 变量与常量
阐述
变量与常量在使用前必须首先声明,声明的时候可以赋值,也可以不赋值。如果不赋值,需要声明它的类型。var
用于声明变量,而 let
用于常量;常量在第一次赋值之后不能再次被赋值。
var environment = "development"
let maximumNumberOfLoginAttempts: Int
// maximumNumberOfLoginAttempts has no value yet.
if environment == "development" {
maximumNumberOfLoginAttempts = 100
} else {
maximumNumberOfLoginAttempts = 10
}
// Now maximumNumberOfLoginAttempts has a value, and can be read.